home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / C⁄C++ OS8 / AMReminder / MainWindow.cp < prev    next >
Encoding:
Text File  |  1998-10-18  |  7.7 KB  |  429 lines  |  [TEXT/CWIE]

  1. // MainWindow.cp
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>
  7. #include <Events.h>
  8. #include <Lists.h>
  9. #include <Menus.h>
  10. #include <Resources.h>
  11. #include <Sound.h>
  12. #include <TextEdit.h>
  13. #include <ToolUtils.h>
  14. #include <Appearance.h>
  15.  
  16. #include "Globals.h"
  17. #include "ResourceDefs.h"
  18. #include "DoScrap.h"
  19. #include "Miscellany.h"
  20. #include "Scrolling.h"
  21. #include "ControlUtils.h"
  22. #include "Add.h"
  23. #include "DDocData.h"
  24. #include "AMReminderEngine.h"
  25. #include "AMReminderDoc.h"
  26.  
  27. #include "MainWindow.h"
  28.  
  29. #include "ReminderPanel.h"
  30.  
  31. //----------
  32. void    MainWindow::Create (
  33.     AMDoc*            inDoc,
  34.     DDocData*        inData)
  35. {
  36.     MainWindow*        winObj = new MainWindow;
  37.  
  38.     if (winObj != nil) {
  39.         winObj->Open (inDoc);
  40.         winObj->ConnectToData (inData);
  41.     }
  42. }
  43.  
  44. //----------
  45. MainWindow::MainWindow ()
  46. {
  47.     mData = nil;
  48. }
  49.  
  50. //----------
  51. MainWindow::~MainWindow ()
  52. {
  53. }
  54.  
  55. //----------
  56. AMReminderEngine*    MainWindow::GetEngine ()
  57. {
  58.     return (AMReminderEngine*) mDoc->mEngine;
  59. }
  60.  
  61. //----------
  62. void    MainWindow::Open (
  63.     AMDoc*            inDoc)
  64. {
  65.     WindowPtr        window;
  66.     Handle            wftb;
  67.  
  68.     mDoc = inDoc;
  69.  
  70.     window = GetNewCWindow (WIND_MainWindow, nil, (WindowPtr) -1L);
  71.     if (mDoc->mEngine->GetFilename () [0] != 0) {
  72.         SetWTitle (window, mDoc->mEngine->GetFilename ());
  73.     }
  74.     mWindow = window;
  75.     ((AMReminderDoc*)mDoc)->mMainWindowPtr = window;
  76.  
  77.     SetWindowKind (window, 'AM');
  78.     SetWRefCon (window, (long) this);
  79.     SetPort (window);
  80.     SetInfo (window);
  81.  
  82.     wftb = ::GetResource ('Wftb', WIND_MainWindow);
  83.  
  84.     CreateRootControl (window, &mRootControl);
  85.  
  86.     vScroll = nil;
  87.     hScroll = nil;
  88.  
  89.  
  90.     mLogoHandle = ::GetNewControl (CNTL_Logo, window);
  91.     SetWindowItemFont (mLogoHandle, wftb, 1);
  92.  
  93.     mYearLabel = GetNewControl (CNTL_Year, window);
  94.     SetWindowItemFont (mYearLabel, wftb, 2);
  95.     SetControlFromTEXT (mYearLabel, TEXT_Year);
  96.  
  97.     mRemindersLabelLabel = GetNewControl (CNTL_RemindersLabel, window);
  98.     SetWindowItemFont (mRemindersLabelLabel, wftb, 3);
  99.     SetControlFromTEXT (mRemindersLabelLabel, TEXT_RemindersLabel);
  100.  
  101.     mRemindersHandle = ::GetNewControl (CNTL_Reminders, window);
  102.     SetWindowItemFont (mRemindersHandle, wftb, 4);
  103.     BuildRemindersList (mRemindersHandle);
  104.  
  105.     mAddHandle = ::GetNewControl (CNTL_Add, window);
  106.     SetWindowItemFont (mAddHandle, wftb, 5);
  107.     SetDefaultState (mAddHandle, true);
  108.  
  109.     mEditHandle = ::GetNewControl (CNTL_Edit, window);
  110.     SetWindowItemFont (mEditHandle, wftb, 6);
  111.  
  112.     mDeleteHandle = ::GetNewControl (CNTL_Delete, window);
  113.     SetWindowItemFont (mDeleteHandle, wftb, 7);
  114.  
  115.     AdvanceKeyboardFocus (window);
  116.  
  117.     ShowWindow (window);
  118. }
  119.  
  120. //----------
  121. void    MainWindow::Close ()
  122. {
  123.     mData->RemoveResponder (this);
  124.  
  125.     ((AMReminderDoc*)mDoc)->mMainWindowPtr = nil;
  126.     SetInfo (nil);
  127.     HideWindow (mWindow);
  128.     DisposeWindow (mWindow);
  129.  
  130.     delete this;
  131. }
  132.  
  133. //----------
  134. void    MainWindow::ConnectToData (
  135.     DDocData*        inData)
  136. {
  137.     mData = inData;
  138.     mData->AddResponder (this);
  139.  
  140.     mRemindersListBox.UseArray (mData->GetReminders ());
  141.     SetListBoxChoice (mRemindersHandle, mData->GetReminderChoice ());
  142.     EnableControl (mEditHandle, mData->GetIsSelected ());
  143.     EnableControl (mDeleteHandle, mData->GetIsSelected ());
  144. }
  145.  
  146. //----------
  147. void    MainWindow::DataChanged (
  148.     long        inDataID)
  149. {
  150.     if (inDataID == idIsSelected) {
  151.         EnableControl (mEditHandle, mData->GetIsSelected ());
  152.     }
  153.     if (inDataID == idIsSelected) {
  154.         EnableControl (mDeleteHandle, mData->GetIsSelected ());
  155.     }
  156. }
  157.  
  158. //----------
  159. void    MainWindow::BuildRemindersList (
  160.     ControlHandle        inControl)
  161. {
  162.     ListHandle        list = GetListHandle (inControl);
  163.  
  164.     AMPanel*        panel = ReminderPanel::Create (mWindow);
  165.     mRemindersListBox.Initialize (list);
  166.     mRemindersListBox.UsePanel (panel);
  167.  
  168. }
  169.  
  170. //----------
  171. void    MainWindow::Control (
  172.     ControlHandle        whichControl,
  173.     short                whichPart,
  174.     Point                where)
  175. {
  176.     Rect            bounds;
  177.     short            newValue;
  178.  
  179.     ExitCurField ();
  180.  
  181.     if (whichControl == mRemindersHandle) {
  182.         if (TrackClick (mRemindersHandle, where)) {
  183.             if (WasDoubleClick (mRemindersHandle)) {
  184.                 DoEditReminder ();
  185.             } else {    // single click
  186.                 mData->SetReminderChoice (GetListBoxChoice (mRemindersHandle));
  187.             }
  188.         }
  189.     }
  190.     if (whichControl == mAddHandle) {
  191.         if (TrackClick (mAddHandle, where)) {
  192.             DoAddReminder ();
  193.         }
  194.     }
  195.     if (whichControl == mEditHandle) {
  196.         if (TrackClick (mEditHandle, where)) {
  197.             DoEditReminder ();
  198.         }
  199.     }
  200.     if (whichControl == mDeleteHandle) {
  201.         if (TrackClick (mDeleteHandle, where)) {
  202.             DoDeleteReminder ();
  203.         }
  204.     }
  205. }
  206.  
  207. //----------
  208. void    MainWindow::MouseIn (
  209.     Point        where,
  210.     short        modifiers)
  211. {
  212.     Rect        bounds;
  213.  
  214. }
  215.  
  216. //----------
  217. void    MainWindow::ExitCurField ()
  218. {
  219.     ControlHandle    focus;
  220.  
  221.     GetKeyboardFocus (mWindow, &focus);
  222.  
  223.     if (focus == nil) {
  224.         // nothing to exit
  225.  
  226.     }
  227. }
  228.  
  229. //----------
  230. void    MainWindow::TypeIn (
  231.     char        ch)
  232. {
  233.     ControlHandle    focus;
  234.     SInt16            keyCode;
  235.  
  236.     GetKeyboardFocus (mWindow, &focus);
  237.  
  238.     if ((ch == charEnter)
  239.     ||  (ch == charReturn)) {
  240.         ExitCurField ();
  241.         SimulateClick (mAddHandle);
  242.         DoAddReminder ();
  243.     } else if (ch == charEsc) {
  244.     } else if (ch == charTab) {
  245.         DoTab ((curEvent.modifiers & optionKey) != 0);
  246.     } else if (focus != nil) {
  247.         keyCode = (SInt16)(curEvent.message & keyCodeMask);
  248.         HandleControlKey (focus, keyCode, ch, (SInt16)curEvent.modifiers);
  249.         mDoc->mEngine->SetDirty ();
  250.     } else {
  251.         SysBeep (1);
  252.     }
  253. }
  254.  
  255. //----------
  256. void    MainWindow::Resize ()
  257. {
  258.     /* application-specific code to resize items in window */
  259. }
  260.  
  261. //----------
  262. void    MainWindow::Scroll (
  263.     short        newValue,
  264.     short        oldValue)
  265. {
  266.     /* application-specific code to scroll window */
  267.     if (gWhichScroll == vScroll) {
  268.     } else {    // horizontal
  269.     }
  270. }
  271.  
  272. //----------
  273. void    MainWindow::DoUndo ()
  274. {
  275. } // DoUndo
  276.  
  277. //----------
  278. void    MainWindow::DoCut ()
  279. {
  280.     TEHandle        curTE = GetCurTE ();
  281.  
  282.     if (curTE != nil) {
  283.         TECut (curTE);
  284.         mDoc->mEngine->SetDirty ();
  285.         scrapDirty = true;
  286.     }
  287. } // DoCut
  288.  
  289. //----------
  290. void    MainWindow::DoCopy ()
  291. {
  292.     TEHandle        curTE = GetCurTE ();
  293.  
  294.     if (curTE != nil) {
  295.         TECopy (curTE);
  296.         scrapDirty = true;
  297.     }
  298. } // DoCopy
  299.  
  300. //----------
  301. void    MainWindow::DoPaste ()
  302. {
  303.     TEHandle        curTE = GetCurTE ();
  304.  
  305.     if (curTE != nil) {
  306.         TEPaste (curTE);
  307.         mDoc->mEngine->SetDirty ();
  308.     }
  309. } // DoPaste
  310.  
  311. //----------
  312. void    MainWindow::DoClear ()
  313. {
  314.     TEHandle        curTE = GetCurTE ();
  315.  
  316.     if (curTE != nil) {
  317.         TEDelete (curTE);
  318.         mDoc->mEngine->SetDirty ();
  319.     }
  320. } // DoClear
  321.  
  322. //----------
  323. void    MainWindow::DoSelectAll ()
  324. {
  325.     TEHandle        curTE = GetCurTE ();
  326.  
  327.     if (curTE != nil) {
  328.         TESetSelect (0, 32767, curTE);
  329.     }
  330. } // DoSelectAll
  331.  
  332. //----------
  333. void    MainWindow::DoShowClipboard ()
  334. {
  335. } // DoShowClipboard
  336.  
  337. //----------
  338. void    MainWindow::DoEditReminder ()
  339. {
  340.     DReminder        data;
  341.  
  342. // hand-written:
  343.     SInt32            index = mData->GetReminderChoice ();
  344.     DReminder*        oldItem = mData->GetReminders ()->GetNthItem (index);
  345.  
  346.     data.CopyFrom (*oldItem);
  347. //
  348.  
  349.     if (CAdd::GetAdd (&data)) {
  350. // hand-written:
  351.         oldItem->CopyFrom (data);
  352.         mData->GetReminders ()->ChangedNth (index);
  353.         GetEngine ()->SetDirty ();
  354. //
  355.     }
  356. }
  357.  
  358. //----------
  359. void    MainWindow::DoAddReminder ()
  360. {
  361.     DReminder        data;
  362.  
  363.     // pre-invoke code
  364.  
  365.     if (CAdd::GetAdd (&data)) {
  366. // hand-written:
  367.         DReminder*        newItem = new DReminder;
  368.  
  369.         newItem->CopyFrom (data);
  370.         mData->GetReminders ()->AddItem (newItem);
  371.         GetEngine ()->SetDirty ();
  372. //
  373.     }
  374. }
  375.  
  376. //----------
  377. void    MainWindow::DoDeleteReminder ()
  378. {
  379.     SInt32            index = mData->GetReminderChoice ();
  380.     mData->GetReminders ()->RemoveNth (index);
  381.     GetEngine ()->SetDirty ();
  382.  
  383. }
  384.  
  385. //----------
  386. Boolean        MainWindow::DoCommand (
  387.     long        inCommand)
  388. {
  389.     Boolean        result = true;
  390.  
  391.     switch (inCommand) {
  392.         case cmdUndo:
  393.                 DoUndo ();
  394.             break;
  395.         case cmdCut:
  396.                 DoCut ();
  397.             break;
  398.         case cmdCopy:
  399.                 DoCopy ();
  400.             break;
  401.         case cmdPaste:
  402.                 DoPaste ();
  403.             break;
  404.         case cmdClear:
  405.                 DoClear ();
  406.             break;
  407.         case cmdSelectAll:
  408.                 DoSelectAll ();
  409.             break;
  410.         case cmdShowClipboard:
  411.                 DoShowClipboard ();
  412.             break;
  413.         case cmdEditReminder:
  414.                 DoEditReminder ();
  415.             break;
  416.         case cmdAddReminder:
  417.                 DoAddReminder ();
  418.             break;
  419.         case cmdDeleteReminder:
  420.                 DoDeleteReminder ();
  421.             break;
  422.  
  423.         default:
  424.                 result = false;
  425.     } // switch
  426.  
  427.     return result;
  428. }
  429.